<?php
require_once 'lib/controller.php';
require_once 'models/Person.php';
require_once 'db/SearchXMLDB.php';
require_once 'views/SearchXMLView.php';
header("Cache-Control: no-cache, must-revalidate"); // HTTP/1.1
header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); // Date in the past
header("content-type:application/xml;charset=utf-8;");
$contentDB = new SearchXMLDB();
$contentView = new SearchXMLView();
/**
 * @return string
 * @param string $lastName
 * @param string $neeName
 */
function lastNameTerms($lastName, $neeName)
{
    $terms = /*.(array[]string).*/ array();
    $lastIsLast   = 'Person.Surname    LIKE "%4$s%%"';
    $neeIsLast    = 'Person.NeeName    LIKE "%4$s%%"';
    $formerIsLast = 'Person.FormerName LIKE "%4$s%%"';
    $lastIsNee    = 'Person.Surname    LIKE "%3$s%%"';
    $neeIsNee     = 'Person.NeeName    LIKE "%3$s%%"';
    $formerIsNee  = 'Person.FormerName LIKE "%3$s%%"';
    if (!empty($lastName)) {
        array_push($terms, "$lastIsLast OR $neeIsLast OR $formerIsLast");
    }
    if (!empty($neeName)) {
        array_push($terms, "$lastIsNee OR $neeIsNee OR $formerIsNee");
    }
    if (!empty($terms)) {
        return '(' . implode(' OR ', $terms) . ')';
    } else {
        return '';
    }
}
/**
 * @return string
 * @param string $firstName
 * @param string $akaName
 */
function firstNameTerms($firstName, $akaName)
{
    $terms = /*.(array[]string).*/ array();
    $firstIsFirst = 'Person.FirstNames  LIKE "%1$s%%"';
    $akaIsFirst   = 'Person.AlsoKnownAs LIKE "%1$s%%"';
    $firstIsAka   = 'Person.FirstNames  LIKE "%2$s%%"';
    $akaIsAka     = 'Person.AlsoKnownAs LIKE "%2$s%%"';
    if (!empty($firstName)) {
        array_push($terms, "$firstIsFirst OR $akaIsFirst");
    }
    if (!empty($akaName)) {
        array_push($terms, "$firstIsAka OR $akaIsAka");
    }
    if (!empty($terms)) {
        return '(' . implode(' OR ', $terms) . ')';
    } else {
        return '';
    }
}
/**
 * @return array[int][string]string
 * @param string $firstName
 * @param string $akaName
 * @param string $lastName
 * @param string $neeName
 */
function getMatchingPeople($firstName, $akaName, $lastName, $neeName)
{
    global $contentDB;
    $people = /*.(array[int][string]string).*/ array();
    if (count(
        array_filter(array($firstName, $akaName, $neeName, $lastName))
    ) > 0) {
        $nameParts = array(
            'firstName' => $firstName,
            'akaName' => $akaName,
            'neeName' => $neeName,
            'lastName' => $lastName
        );
        $pieces = /*.(array[]string).*/ array_filter(array(
            firstNameTerms($firstName, $akaName),
            lastNameTerms($lastName, $neeName)
        ));
        $people = $contentDB->getPeople($nameParts, array(
            'where' => implode(' AND ', $pieces),
            'orderBy' => 'Person.AddedOn DESC',
            'limit' => '0,20'
        ));
    }
    return $people;
}
/**
 * @return string
 * @param array[int][string]string $people
 */
function process($people)
{
    global $html;
    
    $items = '';
    foreach($people as $details) {
        $person = new Person($html);
        $person->set($details);
        $items .= $person->toXML();
    }
    return $items;
}
$firstName = $helper->get('firstname');
$akaName = $helper->get('aka');
$lastName = $helper->get('surname');
$neeName = $helper->get('nee');
$people = getMatchingPeople($firstName, $akaName, $lastName, $neeName);
$content = process($people);
echo $contentView->showXML($content);
?>
